<!--
*@Author: QWP
*@Description: 离职编辑
*@Date: 2024-07-08 17:04:38
-->
<template>
  <u-dialog v-bind="$attrs"
    :confirmLoading="formLoading"
    destroyOnClose
    @submit="submit"
    @close="dialogClose">
    <u-busi-card title=""
      :bordered="false">
      <u-form :elements="formEls"
        :model="form"
        :gutter="16"
        :loading="formLoading"
        @formChange="getUser"
        ref="formRef">
      </u-form>
    </u-busi-card>
  </u-dialog>
</template>

<script lang='ts' setup>
import { ref, reactive, computed, onMounted, h, useAttrs, watch } from 'vue' 
import { useLoginStore,  useBaseStore } from '@/store'
import { storeToRefs } from 'pinia'
import { useRoute, useRouter } from 'vue-router'
import { _IFormEl, _IExtra } from '@/src/components/common/form/interface';
import { FormInstance, FormItemProps } from 'ant-design-vue';
import { HomeOutlined, FileTextOutlined, UploadOutlined } from '@ant-design/icons-vue';
import { TreeProps } from 'ant-design-vue/es/vc-tree';
import { ColumnProps } from 'ant-design-vue/es/table';
import { _IColumnProps } from '@/components/common/tableEdit/interface.ts'
import { fileCols } from '@/common/model/fileCols'
import { cloneDeep } from 'lodash'
import { educations, sexs} from '@/common/model/base'
import { alertC, messageC, numberC, isValue } from '@/util/common'
import { useSetViewPath } from '@/hooks/useFile'
import { insureFormEls, insureRecordCols } from './data/data.ts'
import { getCurrentApi } from '@/views/oa/personnelManager/personInfo/data/api.ts'

interface Props {
  userInfo: object,
  type: string, // 1 离职   2 退休  
}
const props = defineProps<Props>()
const route = useRoute()
const attrs = useAttrs()
const { userInfo } = storeToRefs(useLoginStore())
const { deptData } = storeToRefs(useBaseStore())

const emits = defineEmits(['success'])

const typeLabel = computed(() => {
  let label = { 1: '离职', 2: '退休', }
  return label[form.value.key3] || ''
})

// 表单相关信息
const formRef = ref<FormInstance>()
const form = ref({})
const formLoading = ref<boolean>(false)
const formItemProps: FormItemProps & _IExtra = {
  labelWidth: '100px',
  colon: false
}
const formEls: Array<_IFormEl> = reactive([
  { label: '人员姓名', key: 'perName', col: { span: 24,}, element: 'select', 
    formItemProps: { ...formItemProps, required: true },
    componentProps: { options: [],},
  },
  { label: '类型', key: 'perType', col: { span: 24,}, element: 'select', 
    formItemProps: { ...formItemProps, required: true },
    componentProps: { 
      options: [
        {label: '离职', value: '1'},
        {label: '退休', value: '2'},
      ],
    },
  },
  { label: computed(() => typeLabel.value + '日期'), key: 'takeEffectDate', col: { span: 24,}, element: 'datepicker', 
    formItemProps: { ...formItemProps, required: true },
  },
  { label: computed(() => typeLabel.value + '原因'), key: 'causer', col: { span: 24,}, element: 'textarea', 
    formItemProps: { ...formItemProps, required: true, },
  },
  { label: computed(() => typeLabel.value + '备注'), key: 'remark', col: { span: 24,}, element: 'textarea', 
    formItemProps: { ...formItemProps, required: false, },
  },
  { label: '附件', key: 'appendixSaveVOList', col: { span: 24,}, element: 'file', 
    formItemProps: { ...formItemProps, required: false },
    componentProps: {
      fileParams: {
        biztype: '10', bucket: 'PERSONNEL_BIZ_TYPE_RETIRE',
      }
    }
  },
])

// 确认提交
const submit = async () => {
  try {
    await formRef.value.form.validate()
  } catch (error) {
    messageC("请现将表单信息填写完毕！");
    return
  }

  const api = getCurrentApi('retire');
  if(!api) {
    return
  }

  formLoading.value = true;
  try {
    const res = await api.add([form.value])
    if(res.code == 0) {
      emits('success');
      messageC(res.msg, 'success')
    }
    else {
      messageC(res.msg, 'warning')
    }
  } catch (error) {}
  formLoading.value = false;

}

// 初始化默认保存传递的数据
const initBaseInfo = () => {
  form.value = {
    perInfoNoId: props.userInfo["keyid"] || '',
    perName: props.userInfo["personnelName"] || '',
  }
}

// 弹窗关闭
const dialogClose = () => {
  formRef.value.form.resetFields()
  form.value = {}
}

watch(() => attrs.open, async (nval, oval) => {
  if (nval == true) {
    initBaseInfo()
  }
})
</script>

<style lang='scss' >
</style>